package org.micro.rbac.core.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.annotation.JSONField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * @author hongsir
 * @apiNote 摘要:自定义分页信息
 * @date 2019-03-14 12:41
 */
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class PageBean<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 4889710259477043637L;

    @JSONField(ordinal = 1)
    @Schema(description = "当前页")
    private Integer pageNum = 1;

    @JSONField(ordinal = 2)
    @Schema(description = "页容量,默认10")
    private Integer pageSize = 10;

    @JSONField(ordinal = 3)
    @Schema(description = "分页总数")
    private Long total = 0L;

    @JSONField(ordinal = 4)
    @Schema(description = "列表数据")
    private List<T> list;

    public PageBean(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageBean(Integer pageNum, Integer pageSize, Long total) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
    }

    public PageBean(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }

    public PageBean(List<T> list) {
        if (ObjectUtils.isEmpty(list)) {
            this.pageSize = 0;
            this.total = 0L;
            this.list = null;
        } else {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.total = (long) list.size();
            this.list = list;
        }
    }

    public <Entity extends Serializable> PageBean(List<Entity> list, Class<T> clazz) {
        if (ObjectUtils.isEmpty(list)) {
            this.pageSize = 0;
            this.total = 0L;
            this.list = null;
        } else {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.total = (long) list.size();
            this.list = JSONArray.parseArray(JSON.toJSONString(list), clazz);
        }
    }

}
